﻿using System.Collections.ObjectModel;

namespace PLCS.App.Shared.Extensions;

public static class ListExtensions
{
    public static IEnumerable<T> SearchQuery<T>(this IEnumerable<T> list, string search)
    {
        if (string.IsNullOrEmpty(search))
            return list;
        var result = new List<T>();
        foreach (var model in list)
        {
            Type type = typeof(T);
            foreach (var item in type.GetProperties())
            {
                string itemsearch = "";
                var value = item.GetValue(model);
                if (value == null)
                    continue;
                itemsearch = item.GetValue(model).ToString();
                if (itemsearch.ToLower().Contains(search.ToLower()))
                {
                    result.Add(model);
                    break;
                }
            }
        }
        return result;
    }

    /// <summary>
    /// IEnumerable转Observable
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="from"></param>
    /// <returns></returns>
    public static ObservableCollection<T> ToObservable<T>(this IEnumerable<T> from)
    {
        if (from == null)
            return new ObservableCollection<T>();
        ObservableCollection<T> to = new ObservableCollection<T>();
        foreach (var f in from)
        {
            to.Add(f);
        }
        return to;
    }
}